package com.lxj.fast_android_demo.view;

import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils.TruncateAt;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.lxj.fast_android_demo.R;
import com.lxj.fast_android_demo.dto.DataRecord;
import com.lxj.fast_android_demo.dto.Dataset;

/**
 * 自定义Table
 * 
 * @author LiangXiangJu
 * @creationTime 2014-6-30 下午4:22:50
 * @version
 */
public class MeapTable extends LinearLayout {
	private Context context;
	private Dataset dataSet;

	public MeapTable(Context context, Dataset dataSet) {
		super(context);
		this.context = context;
		this.dataSet = dataSet;
		init();
	}

	public MeapTable(Context context, AttributeSet attrs, Dataset dataSet) {
		super(context, attrs);
		this.context = context;
		this.dataSet = dataSet;
		init();
	}

	public MeapTable(Context context, AttributeSet attrs, int defStyle,
			Dataset dataSet) {
		super(context, attrs, defStyle);
		this.context = context;
		this.dataSet = dataSet;
		init();
	}

	/**
	 * 初始化
	 * 
	 * @author LiangXiangJu
	 * @creationTime 2014-6-29 下午12:59:03
	 * @version
	 */
	private void init() {
		this.setLayoutParams(new LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		this.setOrientation(LinearLayout.VERTICAL);
		this.setBackgroundColor(Color.WHITE);
		LinearLayout tableHead = new LinearLayout(context);
		ListView lvList = new ListView(context);
		this.addView(tableHead);
		this.addView(lvList);

		// 初始化表头
		tableHead.setOrientation(0);
		tableHead.setLayoutParams(new LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		if (null != dataSet) {
			for(String key:dataSet.getFields().keySet()){
				TextView tv = new TextView(context);
				tableHead.addView(tv);
				tv.setTextAppearance(context, R.style.TableHeadStype);
				tv.setWidth(160);
				tv.setSingleLine();
				tv.setPadding(10, 0, 10, 0);
				tv.setGravity(Gravity.CENTER);
				tv.setText(key);
			}
		}

		lvList.setLayoutParams(new LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		ListAdapter adapter = new CustomTableAdapter(dataSet.getRecords());
		lvList.setAdapter(adapter);
	}

	class CustomTableAdapter extends BaseAdapter {
		private List<DataRecord> recordList;

		public CustomTableAdapter(List<DataRecord> recordList) {
			this.recordList = recordList;
		}

		@Override
		public int getCount() {
			return recordList.size();
		}

		@Override
		public DataRecord getItem(int arg0) {
			return recordList.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			LinearLayout lineLayout = new LinearLayout(context);
			lineLayout.setOrientation(0);// 水平布局

			// 根据表头列数建立table行
			if (null != dataSet.getFields()) {
				for (String key:dataSet.getFields().keySet()) {
					TextView tv = new TextView(context);
					lineLayout.addView(tv);
					tv.setTextAppearance(context, R.style.TableColumnStype);
					tv.setSingleLine();
					tv.setWidth(160);
					tv.setPadding(10, 0, 10, 0);
					tv.setGravity(Gravity.CENTER);
					tv.setEllipsize(TruncateAt.END);
					Object textObj = getItem(arg0).getValue(
							key);
					if (null != textObj) {
						tv.setText(String.valueOf(textObj));
					}
				}
			}
			return lineLayout;
		}
	}
}
